{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    1\n",
      "   1 1\n",
      "  1 2 1\n",
      " 1 3 3 1\n",
      "1 4 6 4 1\n"
     ]
    }
   ],
   "source": [
    "def print_yanghui_triangle(n):\n",
    "    # 创建一个二维列表来存储杨辉三角的数据\n",
    "    triangle = [[0] * (i + 1) for i in range(n)]\n",
    "\n",
    "    # 填充杨辉三角的值\n",
    "    for i in range(n):\n",
    "        triangle[i][0] = 1  # 每一行的第一个元素是1\n",
    "        triangle[i][i] = 1  # 每一行的最后一个元素是1\n",
    "        for j in range(1, i):\n",
    "            triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]  # 每个元素是上一行的两个元素之和\n",
    "\n",
    "    # 打印杨辉三角（使用字符' * '来表示数字）\n",
    "    for i in range(n):\n",
    "        # 计算每行的空格数，保证三角形的对称\n",
    "        spaces = ' ' * (n - i - 1)\n",
    "        # 计算每行的数字\n",
    "        row = ' '.join(str(triangle[i][j]) for j in range(i + 1))\n",
    "        print(spaces + row)\n",
    "\n",
    "# 输入杨辉三角的行数\n",
    "n = int(input(\"请输入杨辉三角的行数: \"))\n",
    "print_yanghui_triangle(n)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
